package com.cskaoyan.config4;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author stone
 * @date 2022/06/02 14:59
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class) // 把value属性对应的参数类注册为容器中的组件
public class DataSourceConfiguration4 {

    //@Autowired
    //DataSourceProperties properties;

    DataSourceProperties properties;

    // bean的实例化采用有参构造方法的方式来完成
    // 有参构造方法的形参 → 默认按照类型从容器中取出组件
    public DataSourceConfiguration4(DataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DruidDataSource dataSource4() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
